/**
 * Copyright (C) 2010 kt innotz Co., Ltd. All rights reserved.
 */
package com.kt.paas.prov.iaas.worker;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.kt.paas.prov.iaas.listeners.DefaultIaaSEventListener;
import com.ktinnotz.iaas.exception.IaasException;
import com.ktinnotz.iaas.exception.InvalidConfigurationException;
import com.ktinnotz.iaas.internal.AsyncJobWaiter;
import com.ktinnotz.iaas.services.Iaas;
import com.ktinnotz.iaas.services.IaasCredential;
import com.ktinnotz.iaas.services.IaasLoader;
import com.ktinnotz.iaas.services.model.IaasVMPerfClass;


/**
 * Created on 2011. 2. 17.
 * <p>Title : RequestIaaSWorker.java</p>
 * <p>@author : kobe </p>
 * <p>Description : </p>
 * 
 *
 */
public class RequestIaaSWorker {
	
	private static final Logger logger = LoggerFactory.getLogger(RequestIaaSWorker.class); 
	
	private static Iaas iaas = null;
	private static IaasCredential cred = null;

	private static String templateId = "2";  // centos
	private static String vmid = "";
	private static String apiKey = "14Xst5T7SoxVmMe1xd8YYVn1qTBUH1GjVSLuwGGS6i-lZff9ETsaZwij49xIOUs_MQMx8Ec5JcY6Tj8cAiMWNA";
	private static String secretKey = "SPEJ2QK-l9_Qs9rMOKjKGiwRTcUCttXQRJxcea_ZLtVwHcnzJnYdEwtZOs68tfWnBDk7jWKKW4-iJaAdva9HEw";
	
	@Autowired 
	DefaultIaaSEventListener fDefaultIaaSEventListener;
	
	public RequestIaaSWorker() {
		// 초기화 값
		try {
			initialIaaS();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidConfigurationException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		cred = new IaasCredential(apiKey, secretKey);
	}
	
	private void initialIaaS() throws IOException, InvalidConfigurationException, ClassNotFoundException {
		Properties prop = new Properties();
		prop.load(RequestIaaSWorker.class.getClassLoader().getResourceAsStream("paasIaas.properties"));
		
		IaasLoader loader = new IaasLoader("com.ktinnotz.iaas.services.CSIaas");
		iaas = loader.getIaas(prop);
	}
	
	private void timedlog(String msg) {
		logger.debug(msg);
	}
	
	private final SimpleDateFormat fmdayhour = new SimpleDateFormat( "yyyy-MM-dd(HH:mm:ss)" );
	
	private byte[] getHelloBytes() {
		return ("Hello IaaS! " + fmdayhour.format(new Date())).getBytes();
	}
	
	public void destroyVM(String vmId) throws IaasException {
		iaas.destroyVMAsync(null, cred, vmId, fDefaultIaaSEventListener);
	}
	
	public void stopVM(String vmId) throws Exception {
		timedlog(String.format("Begin stopVM"));
		
		iaas.stopVMAsync(null, cred, vmId, fDefaultIaaSEventListener);
	}
	
	public void deployVM() throws Exception {
		
		timedlog(String.format("Begin deployVM"));
		
		AsyncJobWaiter job = new AsyncJobWaiter();
		vmid = iaas.deployVMAsync( null, cred, IaasVMPerfClass.LOW, templateId, getHelloBytes(),
				fDefaultIaaSEventListener );
		
		timedlog(String.format("After deployVM(, args): vmid = " + vmid)); 
		
		job.waitUntil( 6000000 );
	}
}
